//
//  AIMainTabController.m
//  aixianfeng
//
//  Created by 李江波 on 2016/12/22.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "AIMainTabController.h"
#import "AIMainNavController.h"

#import "AIShoppingCatController.h"
#import "WBTabBar.h"



@interface AIMainTabController ()

@property(nonatomic , assign) NSInteger indexFlag;


@end

@implementation AIMainTabController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    WBTabBar *wbTabBar = [[WBTabBar alloc]init];
    [self setValue:wbTabBar forKey:@"tabBar"];
    wbTabBar.btnClickBlock = ^{
        
        [self presentViewController:[[AIShoppingCatController alloc]init] animated:YES completion:nil];
    };
    
    NSDictionary *dict1 = @{@"clz" : @"AIHomeController" ,@"title" : @"首页" ,@"img" : @"v2_home" ,@"tag" : @(1)};
    NSDictionary *dict2 = @{@"clz" : @"AIMaeketController" ,@"title" : @"闪电超市" ,@"img" : @"v2_order",@"tag" : @(2)};
    NSDictionary *dict4 = @{@"clz" : @"AIMineController" ,@"title" : @"我的" ,@"img" : @"v2_my",@"tag" : @(4)};
    
    NSArray *array = @[dict1,dict2,dict4];
    
    NSMutableArray *arrayM = [NSMutableArray array];
    
    [array enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIViewController *vc = [self viewControllerWithDictionary:obj];
        
        [arrayM addObject:vc];
    }];
    
    self.viewControllers = arrayM.copy;
  
    
    
}

-(AIMainNavController *)viewControllerWithDictionary:(NSDictionary *)dict{
    
    Class clz = NSClassFromString(dict[@"clz"]);
    UIViewController *vc = [[clz alloc]init];
    
    vc.tabBarItem.title = dict[@"title"];
    
    vc.tabBarItem.tag = [dict[@"tag"] integerValue];
    //FFD700
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor]} forState:UIControlStateSelected];
    
    vc.tabBarItem.image = [[UIImage imageNamed:dict[@"img"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_r",dict[@"img"]]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    AIMainNavController *nav = [[AIMainNavController alloc]initWithRootViewController:vc];
    
    return nav;
}

//实现tabbar点击动画
//首页item点击动画效果
//首页item点击动画效果
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    NSInteger index = 1;
    for (UIView *subview in tabBar.subviews) {
        
        if ([subview isMemberOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            if (item.tag == index) {
     
                for (UIView *v in subview.subviews) {
                    if ([v isMemberOfClass:NSClassFromString(@"UITabBarSwappableImageView")] ) {
                        
                        v.transform = CGAffineTransformMakeScale(0.6, 0.6);
                        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:8 options:UIViewAnimationOptionCurveEaseIn animations:^{
                            
                            v.transform = CGAffineTransformMakeScale(1, 1);
                            
                        } completion:^(BOOL finished) {
                            
                        }];
                    }
                }
                
                
            }
            index ++ ;
            _indexFlag = index;
        }
    }
}




@end
